﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Xml.Linq;
using Microsoft.SharePoint;

namespace RSM.Business
{
    public class ContentManager
    {
        public ContentManager()
        {
        }
        public List<Fields> GetContentTypeFields(string contentTypeName)
        {

            DataTable dt = Xmlreader.GetXML();
            string xml = string.Empty;
            List<XElement> contentTypesList = new List<XElement>();
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    if (row[column].ToString().StartsWith("<ContentType"))
                    {
                        xml = row[column].ToString();
                        contentTypesList.Add(XmlGenerator.GetElement(xml));
                    }
                }
            }
            List<Fields> fileds = GetContentFields(contentTypesList, contentTypeName);
            return fileds;

        }
        private List<Fields> GetContentFields(List<XElement> list, string contentTypeName)
        {
            string pageName = string.Empty;
            string contentTypeId = string.Empty;
            XNamespace ns = "http://schemas.microsoft.com/sharepoint/";
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-16", "true"));
            XElement Elements = new XElement(ns + "Elements");

            List<XAttribute> pageAttributes = new List<XAttribute>();
            List<Fields> fieldNames = new List<Fields>();
            foreach (XElement ele in list)
            {
                if (ele.ToString().StartsWith("<ContentType"))
                {
                    XName cName = "Name";
                    XAttribute attrib = ele.Attribute(cName);
                    pageName = attrib.Value;
                    XName xId = "ID";
                    XAttribute idAttrib = ele.Attribute(xId);
                    contentTypeId = idAttrib.Value;

                }
                ele.SetAttributeValue("WebId", null);
                ele.SetAttributeValue("Version", null);
                ele.SetAttributeValue("UserSelectionMode", null);
                ele.SetAttributeValue("UserSelectionScope", null);
                if (pageName == contentTypeName)
                {
                    //foreach (XElement el in ele.DescendantsAndSelf())
                    //{
                    fieldNames.Add(new Fields { Type = "pageName", FieldName = pageName });
                    fieldNames.Add(new Fields { Type = "contentTypeID", FieldName = contentTypeId });

                    foreach (XElement elem in ele.DescendantsAndSelf())
                    {
                        elem.Name = ns.GetName(elem.Name.LocalName);
                        List<XAttribute> atList = elem.Attributes().ToList();

                        if (elem.ToString().StartsWith("<FieldRef"))
                        {
                            XName name = "ID";
                            XAttribute attrib = elem.Attribute(name);
                            if (attrib != null)
                            {
                                string id = attrib.Value;

                                XName fieldName = "Name";
                                XAttribute fieldAtt = elem.Attribute(fieldName);


                                XName type = "Type";
                                XAttribute typeAtt = elem.Attribute(type);

                                string fieldN = fieldAtt.Value;

                                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                                {
                                    using (SPWeb web = site.RootWeb)
                                    {
                                        SPFieldCollection collection = web.Fields;
                                        foreach (SPField field in collection)
                                        {
                                            string fieldId = field.Id.ToString();
                                            fieldId = "{" + fieldId + "}";
                                            if (fieldId == id)
                                            {
                                                if (fieldN != "ContentType")
                                                {
                                                    if (field.Type.ToString() != "Invalid")
                                                    {
                                                        fieldNames.Add(new Fields { Type = field.Type.ToString(), FieldName = fieldN });
                                                    }
                                                    else
                                                    {
                                                        fieldNames.Add(new Fields { Type = field.TypeAsString, FieldName = fieldN });

                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                            //}
                        }
                    }
                }
            }
            return fieldNames;
        }
    }
}
